/*
Validate if a given string is numeric.

Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
*/

class Solution {
public:
    bool isNumber(const char *s) {
        // remove ws
        while (*s == ' ') s++;
        if (*s == '+' || *s == '-') s++;
        bool hasdigit, hasdot, hasexp;
        hasdigit = hasdot = hasexp = false;
        while (*s != 0) {
            if (*s >= '0' && *s <= '9') {
                hasdigit = true;
            } else if (*s == '.') {
                if (hasdot || hasexp) return false;
                hasdot = true;
            } else if (*s == 'e') {
                if (hasexp || !hasdigit) return false;
                hasexp = true;
                hasdigit = false; // needs more digit
                s++; while (*s == ' ') s++;
                if (*s != '+' && *s != '-') continue;
            } else {
                // handle tws
                while (*s == ' ') s++;
                if (*s != 0) return false;
                else continue;
            }
            s++;
        }
        return hasdigit;
    }
};
